package com.zsj.service;

import com.zsj.mysql.service.IAppProductPriceService;
import com.zsj.util.MoneyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @Description:
 * @Author:
 * @Date:
 */
@Slf4j
@Service
public class DiscountService {

    @Autowired
    private IAppProductPriceService appProductPriceService;

    public String calculateSelfPrice(String appId, String carrier, String product, String adCode) {
        log.info("开始查询产品折扣，appId:{},carrierCode:{},productDenomination:{},adCode:{}", appId, carrier, product, adCode);
        // 自己的结算金额，单位为毫
        String selfPrice;
        // 将订单金额从分转为毫
        String orderAmtHao = MoneyUtil.fenToHao(product);
        // 查询产品折扣
        BigDecimal discount = appProductPriceService.findDiscount(appId, carrier, product, adCode);
        log.info("折扣为:{}", discount);
        if (null == discount) {
            log.info("appId:{},carrierCode:{},productDenomination:{},adCode:{}未配置折扣",
                    appId, carrier, product, adCode);
            return null;
        } else {
            selfPrice = MoneyUtil.discountedAmt(orderAmtHao, discount);
        }
        return selfPrice;
    }
}
